package com.mh.www.mh_frame.view.activity;

import android.Manifest;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.mh.www.mh_frame.R;
import com.mh.www.mh_frame.model.bean.FileUtils;
import com.mh.www.mh_frame.model.bean.HttpCallBack;
import com.mh.www.mh_frame.model.bean.L;
import com.mh.www.mh_frame.model.bean.MeetingSummaryBean;
import com.mh.www.mh_frame.presenter.iview.MeetingSummaryView;
import com.mh.www.mh_frame.presenter.present.MeetingSummaryPresent;
import com.mh.www.mh_frame.presenter.present.impl.MeetingSummaryPresentImpl;
import com.mh.www.mh_frame.view.activity.base.BaseActivity;
import com.mh.www.mh_frame.view.adapter.MeetingSummaryAdapter2;
import com.mylhyl.acp.Acp;
import com.mylhyl.acp.AcpListener;
import com.mylhyl.acp.AcpOptions;
import com.orhanobut.logger.Logger;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;
import okhttp3.ResponseBody;

/**
 * 会议纪要
 * Created by lenovo on 2018/4/19.
 */

public class MeetingSummaryActivity extends BaseActivity implements MeetingSummaryView {
    @BindView(R.id.ry_meeting_summary)
    RecyclerView mRyMeetingSummary;
    private List<MeetingSummaryBean.DataBean> mMeetingSummaryBeanList;
    private MeetingSummaryPresent mMeetingSummaryPresent;
    private MeetingSummaryAdapter2 mAdapter;

    @Override
    protected void setContentLayout() {
        setContentView(R.layout.activity_meet_summary);
    }

    @Override
    protected void initView() {
        mMeetingSummaryPresent = new MeetingSummaryPresentImpl();
        mMeetingSummaryPresent.attachView(this);
        mMeetingSummaryPresent.getSummary("", "1", "2", "5894630b-dffc-4e93-bfe5-5e923252fbd6");
        mRyMeetingSummary.setLayoutManager(new LinearLayoutManager(this));
    }

    @Override
    protected void obtainData() {

    }

    @Override
    protected void initEvent() {

    }

    @Override
    public void showProgress() {

    }

    @Override
    public void hideProgress() {

    }

    @Override
    public void showMsg(String message) {

    }

    @Override
    public void navigateToHome() {

    }

    @Override
    public void setData(MeetingSummaryBean bean) {
        mMeetingSummaryBeanList = new ArrayList<>();
        for (MeetingSummaryBean.DataBean dataBean : bean.getData()) {
            mMeetingSummaryBeanList.add(dataBean);
        }
        mRyMeetingSummary.setAdapter(mAdapter = new MeetingSummaryAdapter2(mMeetingSummaryBeanList));
        mAdapter.notifyDataSetChanged();
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                //检查权限并下载pdf
                showLoadingDialog();
                L.d("vivi", "文件名---" + mMeetingSummaryBeanList.get(position).getFILENAME());
                mMeetingSummaryPresent.getLoadPDF(mMeetingSummaryBeanList.get(position).getFILENAME());

            }
        });
    }

    @Override
    public void setPDF(ResponseBody responseBody) {
        getPermission(responseBody);
    }

    public void getPermission(final ResponseBody responseBody) {
        Acp.getInstance(this)
                .request(new AcpOptions.Builder()
                                .setPermissions(Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE).build(),
                        new AcpListener() {
                            @Override
                            public void onGranted() {
                                Logger.e("onGranted---" + "有权限");
                                //下载文件
                                load(responseBody);
                            }

                            @Override
                            public void onDenied(List<String> permissions) {
                                Logger.e("onDenied---" + "权限获取被拒绝");
                            }
                        });
    }

    public void load(final ResponseBody responseBody) {
        //建立一个文件
        final File file = FileUtils.createFile(MeetingSummaryActivity.this);

        //下载文件放在子线程
        new Thread() {
            @Override
            public void run() {
                super.run();
                //保存到本地
                FileUtils.writeFile2Disk(MeetingSummaryActivity.this, responseBody, file, new HttpCallBack() {
                    @Override
                    public void onLoading(final long current, final long total) {
                        /**
                         * 更新进度条
                         */
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                dismissLoadingDialog();
                                L.d("vivi", current + " to " + total);
                                L.d("vivi", " runOnUiThread  " + currentThread().getName());

                            }
                        });
                    }
                });


            }
        }.start();
    }

}
